home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / pcl4c60.zip / SELFTEST.C < prev    next >
Text File  |  1996-10-24  |  11KB  |  382 lines

  1. /*
  2. **                ---  selftest.c ---
  3. **
  4. **  SELFTEST requires either a single serial port with a loop-back dongle
  5. **  (connect TX to RX, DTR to DSR, and RTS to CTS), or two serial port
  6. **  on the same computer.
  7. **
  8. **  The program transmits a test string on one port (FirstCOM) and
  9. **  receives on a second port (SecondCOM), where the two ports are
  10. **  connected via a null modem adapter. The received string is tested
  11. **  against the transmit string (they should be idenical).
  12. **
  13. **  Connect the two serial ports (on a single computer) together
  14. **  using a null modem cable. Be sure to modify the configuration
  15. **  section for non-standard PC ports or to setup your multiport
  16. **  board. Note that many multiport boards are either Digiboard or
  17. **  BOCA board compatible.
  18. **
  19. **  IMPORTANT: You may have to modify the port address & IRQ to match
  20. **  your Digiboard or BOCA board installation.
  21. **
  22. **  Compile with /DDPMI for protected mode testing.
  23. */
  24.  
  25. #ifdef DPMI
  26. #include "windows.h"
  27. #include "use_dpmi.h"
  28. #endif
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <malloc.h>
  32. #include <dos.h>
  33. #include <string.h>
  34. #include <conio.h>
  35. #include "pcl4c.h"
  36.  
  37. #define ESC 0x1b
  38. #define CR  0x0d
  39. #define LF  0x0a
  40.  
  41. #define READ_PORT(P) inportb(X)
  42.  
  43. #define PC 1
  44. #define DB 2
  45. #define BB 3
  46. #define CC 4
  47.  
  48. /*** Global Variables ***/
  49.  
  50. #define TEST_SIZE 63
  51.  
  52. static int BaudCode;
  53.  
  54. char TestSet[TEST_SIZE+1];
  55. int  NbrRuns = 16;
  56. int  TestLength;
  57. int FirstCOM;
  58. int SecondCOM;
  59.  
  60. int MultiUART = 0;
  61. int MultiIRQ = 0;
  62. int MultiStatus = 0;
  63.  
  64. void AllDone(void);
  65. void Display(int);
  66. void CheckAlloc(int,char *);
  67. int  ErrorCheck(int);
  68. int  AllocSeg(int);
  69. int  BaudMatch(char *);
  70.  
  71. static int  RxSeg1 = 0;
  72. static int  TxSeg1 = 0;
  73. static int  RxSeg2 = 0;
  74. static int  TxSeg2 = 0;
  75.  
  76. char *Ptr[4] = {"SMALL","COMPACT","MEDIUM","LARGE"};
  77.  
  78. /* standard PC port configuration */
  79.  
  80. static int StdAdrPC[4] = {0x3f8,0x2f8,0x3e8,0x2e8};
  81. static int StdIrqPC[4] = {4,3,4,3};
  82. static char *BaudRate[10] =  {"300","600","1200","2400","4800","9600",
  83.                               "19200","38400","57600","115200"};
  84. static int Selection = 0;  /* PC, DB, or BB */
  85.  
  86. void main(int argc, char *argv[])
  87. {int Port;
  88.  char *P;
  89.  int ComLimit = 0;
  90.  char c;
  91.  int Version;
  92.  int Model;
  93.  int i, n, rc;
  94.  int RxBase;
  95.  int TxBase;
  96.  if(argc!=5)
  97.   {printf("Usage: selftest {pc|db|bb|cc} 1stCom 2ndCom baudrate\n");
  98.    exit(1);
  99.   }
  100.  BaudCode = BaudMatch(argv[4]);
  101.  if(BaudCode==-1)
  102.   {printf("Bad baud rate '%s'\n",argv[4]);
  103.    exit(1);
  104.   }
  105.  P = argv[1];
  106.  if((strcmp(P,"pc")==0)||(strcmp(P,"PC")==0)) Selection = PC;
  107.  if((strcmp(P,"db")==0)||(strcmp(P,"DB")==0)) Selection = DB;
  108.  if((strcmp(P,"bb")==0)||(strcmp(P,"BB")==0)) Selection = BB;
  109.  if((strcmp(P,"cc")==0)||(strcmp(P,"CC")==0)) Selection = CC;
  110.  if(Selection==0)
  111.    {puts("Must specify 'PC', 'DB' or 'BB' as 1st argument");
  112.     puts("EG:  SELFTEST PC 1 4");
  113.     exit(1);
  114.    }
  115.  if(Selection==PC) ComLimit = COM4;
  116.  if(Selection==DB) ComLimit = COM4;
  117.  if(Selection==BB) ComLimit = COM4;
  118.  if(Selection==CC) ComLimit = COM4;
  119.  
  120.  FirstCOM = atoi(argv[2]) -1;
  121.  SecondCOM = atoi(argv[3]) -1;
  122.  printf("FirstCOM  = COM%d\n",1+FirstCOM);
  123.  printf("SecondCOM = COM%d\n",1+SecondCOM);
  124.  if(FirstCOM<COM1)
  125.    {puts("1stCom must be >= COM1");
  126.     exit(1);
  127.    }
  128.  if(SecondCOM>ComLimit)
  129.    {printf("2ndCom must be <= COM%d\n",1+ComLimit);
  130.     exit(1);
  131.    }
  132.  if(Selection==CC)
  133.    {/*** Custom Configuration: 4 port board ***/
  134.     printf("[ Custom configuration: 0x280,288,290,298: IRQ10,15,10,15]\n");
  135.     MultiUART = 0x280; MultiIRQ = IRQ10; MultiStatus = 0;
  136.     SioUART(COM1,0x280); SioIRQ(COM1,IRQ10);
  137.     SioUART(COM2,0x288); SioIRQ(COM2,IRQ15);
  138.     SioUART(COM3,0x290); SioIRQ(COM3,IRQ10);
  139.     SioUART(COM4,0x298); SioIRQ(COM4,IRQ15);
  140.    }
  141.  if(Selection==DB)
  142.    {/*** Custom Configuration: DigiBoard PC/8 ***/
  143.     MultiUART = 0x180; MultiIRQ = IRQ5; MultiStatus = 0x1C0;
  144.     printf("[ Configuring DigiBoard as COM1-COM8 (IRQ%d) @ 0x%x ]\n",
  145.        MultiIRQ,MultiUART);
  146.     SioPorts(1+ComLimit,COM1,MultiStatus,DIGIBOARD);
  147.     for(Port=COM1;Port<=ComLimit;Port++)
  148.       {/* set DigiBoard UART addresses */
  149.        ErrorCheck( SioUART(Port,MultiUART+8*Port) );
  150.        /* set DigiBoard IRQ */
  151.        ErrorCheck( SioIRQ(Port,MultiIRQ) );
  152.       }
  153.    }
  154.  if(Selection==BB)
  155.    {/*** Custom Configuration: BOCA BB2016 ***/
  156.     MultiUART = 0x100; MultiIRQ = IRQ10; MultiStatus = MultiUART + 7;
  157.     printf("[ Configuring BOCA Board as COM1-COM16 (IRQ%d) @ 0x%x ]\n",
  158.        MultiIRQ,MultiUART);
  159.     SioPorts(1+ComLimit,COM1,MultiStatus,BOCABOARD);
  160.     for(Port=COM1;Port<=ComLimit;Port++)
  161.       {/* set BOCA Board UART addresses */
  162.        ErrorCheck( SioUART(Port,MultiUART+8*Port) );
  163.        /* set BOCA Board IRQ */
  164.        ErrorCheck( SioIRQ(Port,MultiIRQ) );
  165.       }
  166.    }
  167.  if(Selection==PC)
  168.    {/*** Standard Configuration: 4 port card ***/
  169.     puts("[ Configuring for PC ]");
  170.     for(i=COM1;i<=COM4;i++)
  171.       {SioUART(i,StdAdrPC[i]);
  172.        SioIRQ(i,StdIrqPC[i]);
  173.       }
  174.    }
  175.  CheckAlloc(RxSeg1 = AllocSeg(1024), "RX1");
  176.  ErrorCheck( SioRxBuf(FirstCOM,RxSeg1,Size1024) );
  177.  if(SioInfo('I'))
  178.    {CheckAlloc(TxSeg1 = AllocSeg(1024), "TX1");
  179.     ErrorCheck( SioTxBuf(FirstCOM,TxSeg1,Size1024) );
  180.     ErrorCheck( SioParms(FirstCOM,NoParity,OneStopBit,WordLength8) );
  181.    }
  182.  ErrorCheck( SioParms(FirstCOM,NoParity,OneStopBit,WordLength8) );
  183.  ErrorCheck( SioReset(FirstCOM,BaudCode) );
  184.  printf("COM%d reset\n",1+FirstCOM);
  185.  SioRxClear(FirstCOM);
  186.  SioTxClear(FirstCOM);
  187.  
  188.  if(FirstCOM!=SecondCOM)
  189.    {/* setup 2nd port (SecondCOM) */
  190.     CheckAlloc(RxSeg2 = AllocSeg(1024), "RX2");
  191.     ErrorCheck( SioRxBuf(SecondCOM,RxSeg2,Size1024) );
  192.     if(SioInfo('I'))
  193.       {CheckAlloc(TxSeg2 = AllocSeg(1024), "TX2");
  194.        ErrorCheck( SioTxBuf(SecondCOM,TxSeg2,Size1024) );
  195.       }
  196.     ErrorCheck( SioParms(SecondCOM,NoParity,OneStopBit,WordLength8) );
  197.     ErrorCheck( SioReset(SecondCOM,BaudCode) );
  198.     printf("COM%d reset\n",1+SecondCOM);
  199.     SioRxClear(SecondCOM);
  200.     SioTxClear(SecondCOM);
  201.    }
  202.  printf("***\n");
  203.  printf("*** SELFTEST 6.0\n");
  204.  printf("***\n");
  205.  Version = SioInfo('V');
  206.  Model = SioInfo('M');
  207.  printf("*** Lib Ver : %d.%d\n",Version/16,Version%16);
  208.  printf("***   Model : %s \n", Ptr[Model&3] );
  209.  printf("*** TX Intr : ");
  210.  if(SioInfo('I')) puts("Enabled.");
  211.  else puts("Disabled.");
  212.  printf("*** DPMI    : ");
  213.  if(SioInfo('P')) printf("YES\n");
  214.  else printf("NO\n");
  215.  /* display port info */
  216.  Display(FirstCOM);
  217.  Display(SecondCOM);
  218.  printf("***\n");
  219.  
  220.  printf("Starting @ %s baud. COM%d to COM%d\n",argv[4],FirstCOM+1,SecondCOM+1);
  221.  
  222.  TxBase = SioInfo('T');
  223.  RxBase = SioInfo('R');
  224.  /* build TestSet[] array */
  225.  for(i=0;i<26;i++) TestSet[i] = 'A'+i;
  226.  for(i=0;i<26;i++) TestSet[26+i] = 'a'+i;
  227.  for(i=0;i<10;i++) TestSet[52+i] = '0'+i;
  228.  TestSet[62] = '\n';
  229.  TestSet[63] = '\0';
  230.  printf("Test Set: %s",TestSet);
  231.  /* send test sets */
  232.  printf("\n  Sending set: ");
  233.  for(i=0;i<NbrRuns;i++)
  234.    {/* send test set again */
  235.     printf("%d ",i);
  236.     for(n=0;n<TEST_SIZE;n++)
  237.       {c = TestSet[n];
  238.        SioPutc(FirstCOM,c);
  239.       }
  240.    }
  241.  /* receive test sets */
  242.  printf("\nReceiving set: ");
  243.  for(i=0;i<NbrRuns;i++)
  244.    {/* receive next test set */
  245.     for(n=0;n<TEST_SIZE;n++)
  246.       {rc = SioGetc(SecondCOM,18);
  247.        if(rc<0)
  248.          {printf("\nERROR: ");
  249.           SioError(rc);
  250.           printf("%d bytes in COM%d RX queue\n", SioRxQue(FirstCOM),1+FirstCOM );
  251.           printf("%d bytes in COM%d RX queue\n", SioRxQue(SecondCOM),1+SecondCOM );
  252.           printf("%d RX interrupts\n",SioInfo('R') - RxBase );
  253.           SioDone(FirstCOM);
  254.           if(FirstCOM!=SecondCOM) SioDone(SecondCOM);
  255.           exit(1);
  256.          }
  257.        /* compare character */
  258.        if((char)rc!=TestSet[n])
  259.          {printf("\nERROR: Expecting '%c'(0x%x), received '%c'(0x%x)\n",
  260.             (char)rc,(char)rc,TestSet[n],TestSet[n]);
  261.           SioDone(FirstCOM);
  262.           if(FirstCOM!=SecondCOM) SioDone(SecondCOM);
  263.           exit(1);
  264.          }
  265.       }
  266.     printf("%d ",i);
  267.    } /* end for */
  268.  printf("\n\n");
  269.  /* check RX FIFO */
  270.  TestLength = NbrRuns * TEST_SIZE;
  271.  i = SioInfo('R');
  272.  printf("%3d RX interrupts on %d incoming bytes: ", i-RxBase,TestLength);
  273.  if(i-RxBase<TestLength) puts("RX FIFO is operational");
  274.  else puts("RX FIFO is NOT operational [or not 16550 UART]");
  275.  if(SioInfo('I'))
  276.    {/* check TX FIFO */
  277.     i = SioInfo('T');
  278.     printf("%3d TX interrupts on %d outgoing bytes: ", i-TxBase,TestLength);
  279.     if(i-TxBase<TestLength) puts("TX FIFO is operational");
  280.     else puts("TX FIFO is NOT operational [or not 16550 UART]");
  281.    }
  282.  puts("SUCCESS: Test AOK !");
  283.  AllDone();
  284.  exit(0);
  285. } /* end main */
  286.  
  287. int ErrorCheck(int Code)
  288. {/* trap PCL error codes */
  289.  if(Code<0)
  290.      {SioError(Code);
  291.       AllDone();
  292.       exit(1);
  293.      }
  294.  return(0);
  295. } /* end ErrorCheck */
  296.  
  297.  
  298. void Display(int Port)
  299. {printf("***    COM%d : ",1+Port);
  300.  if(Selection==PC) printf("Adr=%3x IRQ=%d",StdAdrPC[Port],StdIrqPC[Port]);
  301.  else printf("Adr=%3xh IRQ=%d Status=%xh",MultiUART+8*(Port-COM1),MultiIRQ,MultiStatus);
  302.  if( SioFIFO(Port,LEVEL_8) ) printf(" [16550]\n");
  303.  else printf(" [8250/16450]\n");
  304. }
  305.  
  306.  
  307. #ifdef DPMI
  308.  
  309. int AllocSeg(int Size)
  310. {long hMem;
  311.  int  Selector = 0;
  312.  hMem = (long) GlobalDosAlloc( (long)Size );
  313.  if(hMem)
  314.    {/* get selector */
  315.     Selector = LOWORD(hMem);
  316.     GlobalPageLock(Selector);
  317.     return Selector;
  318.    }
  319.  else return 0;
  320. }
  321.  
  322. #else
  323.  
  324. int AllocSeg(int Size)
  325. {int Seg;
  326.  char far *Ptr;
  327.  /* allocate far heap */
  328.  Ptr = (char far *) _fmalloc(Size+16);
  329.  if(Ptr==NULL) return 0;
  330.  /* SEG:0 points to buffer */
  331.  Seg = FP_SEG(Ptr) + ((FP_OFF(Ptr)+15)>>4);
  332.  return Seg;
  333. }
  334.  
  335. #endif
  336.  
  337. void AllDone(void)
  338. {printf("Shutting down COM%d, ",1+FirstCOM);
  339.  SioDone(FirstCOM);
  340.  printf("COM%d.\n",1+SecondCOM);
  341.  if(FirstCOM!=SecondCOM) SioDone(SecondCOM);
  342. #ifdef DPMI
  343.  printf("Freeing DOS memory: ");
  344.  /* free DOS memory */
  345.  if(RxSeg1)
  346.     {printf("RX1 ");
  347.      GlobalPageUnlock(RxSeg1);
  348.      GlobalDosFree(RxSeg1);
  349.     }
  350.  if(TxSeg1)
  351.     {printf("TX1 ");
  352.      GlobalPageUnlock(TxSeg1);
  353.      GlobalDosFree(TxSeg1);
  354.     }
  355.  if(RxSeg2)
  356.     {printf("RX2 ");
  357.      GlobalPageUnlock(RxSeg2);
  358.      GlobalDosFree(RxSeg2);
  359.     }
  360.  if(TxSeg2)
  361.     {printf("TX2 ");
  362.      GlobalPageUnlock(TxSeg2);
  363.      GlobalDosFree(TxSeg2);
  364.     }
  365.  #endif
  366.  exit(0);
  367. }
  368.  
  369. void CheckAlloc(int Seg,char *Msg)
  370. {if(Seg==0)
  371.   {printf("Cannot allocate memory for %s\n",Msg);
  372.    AllDone();
  373.    exit(1);
  374.   }
  375. }
  376.  
  377. int BaudMatch(char *P)
  378. {int i;
  379.  /* find baud rate in table */
  380.  for(i=0;i<10;i++) if(strcmp(BaudRate[i],P)==0) return(i);
  381.  return(-1);
  382. }